import requests
from pyquery import PyQuery as pq
from sql_helper import MySqlHelper
import re



def read_list(url, item_all=None):
    if item_all is None:
        item_all = []
        response = requests.get(url)
        if response.status_code == 200:
            box = pq(url=url).find('#content > div > div.article > div.indent > span').items()
            for i in box:
                text = str(i.text())
                list = re.findall(r"[\u4e00-\u9fa5]{2,}[(]{1}\d*[)]{1}|[A-z]{1,}[(]{1}\d*[)]{1}", text)
                for i in list:
                    l = []
                    l.append(re.findall(r"[\u4e00-\u9fa5]{2,}|[A-z]{1,}", i))
                    l.append(re.findall(r"[(]{1}\d*[)]{1}", i))
                    item_all.append(l)
                print('ok')
        else:
            print("你抓的出问题了！")
    return item_all




if __name__ == '__main__':
    url = 'https://music.douban.com/artists/tag/'
    new_list = read_list(url)
    mysql = MySqlHelper()
    sql = "INSERT INTO m_styles(style, number) VALUES(%s,%s)"
    mysql.exec_many(sql, new_list)